package com.linyubing.servlet;

import com.alibaba.fastjson.JSON;
import com.linyubing.dao.BaseDao;
import com.linyubing.dao.BaseDaoImpl;
import com.linyubing.pojo.Course;
import com.linyubing.pojo.StuCourse;
import com.linyubing.pojo.Student;
import com.linyubing.pojo.vo.CourseStudents;
import com.linyubing.pojo.vo.StudentCourse;
import com.linyubing.pojo.vo.StudentCourses;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/studentcourse/*")
public class StudentCourseServlet extends BaseServlet {
    //创建BaseDaoImpl对象
    BaseDao<StuCourse> dao = new BaseDaoImpl<>(StuCourse.class);
    BaseDao<Student> studentDao = new BaseDaoImpl<>(Student.class);
    BaseDao<Course> courseDao = new BaseDaoImpl<>(Course.class);
    public void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //查询所有的选课信息
        List<StuCourse> stuCourses = dao.getAll();
        List<StudentCourse> list=new ArrayList<StudentCourse>();
        for (StuCourse entity : stuCourses) {
            //获取每一个entity的student,course的id信息
            Integer stuId = entity.getStuId();
            Integer courseId = entity.getCourseId();
            //根据id查询相应的实体
            Student student = studentDao.getById(stuId);
            Course course = courseDao.getById(courseId);
            //创建实体对象
            StudentCourse studentCourse = new StudentCourse(entity.getId(), student.getStuName(),
                    course.getCourseName(), entity.getScore());
            //添加到集合中
            list.add(studentCourse);
        }
            //转换为json
            String jsonString = JSON.toJSONString(list);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);

    }
    public void getByStuId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取请求参数的stuId
        int stuId = Integer.parseInt(req.getParameter("stuId"));
        StuCourse stuCourse = new StuCourse();
        stuCourse.setStuId(stuId);
        //首先创建StudentCourses对象
        StudentCourses studentCourses = new StudentCourses();
        //条件查询出符合条件的集合(需要判断是否为null)
        List<StuCourse> stuCourses = dao.getByCondition(stuCourse);
        //根据stuId获取学生信息
        Student student = studentDao.getById(stuCourse.getStuId());
        //设置学生名字
        studentCourses.setStuName(student.getStuName());
        Map<String,Integer> map=new HashMap<String,Integer>();
        if(stuCourses.size()==0){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("fail");
        }else {
            int sum = 0;
            for (StuCourse stuCourse2 : stuCourses) {
                //根据课程id查询课程
                Course course = courseDao.getById(stuCourse2.getCourseId());
                map.put(course.getCourseName(), stuCourse2.getScore());
                sum += stuCourse2.getScore();
            }
            studentCourses.setMap(map);
            studentCourses.setAvg((Integer) sum / stuCourses.size());
            //转换为json
            String jsonString = JSON.toJSONString(studentCourses);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }

    }
    public void getInfoByCourseId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取请求参数的courseId
        int courseId = Integer.parseInt(req.getParameter("courseId"));
        StuCourse stuCourse = new StuCourse();
        stuCourse.setCourseId(courseId);
        //首先创建CourseStudents对象
        CourseStudents courseStudents = new CourseStudents();
        Map<String,Integer> map=new HashMap<String,Integer>();
        //根据courseId获取课程信息
        Course course = courseDao.getById(stuCourse.getCourseId());
        //设置课程名
        courseStudents.setCourseName(course.getCourseName());
        //条件查询出符合条件的集合
        List<StuCourse> stuCourses = dao.getByCondition(stuCourse);
        if(stuCourses.size()==0){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("fail");
        }else{
            int sum=0;
            for (StuCourse stuCourse2 : stuCourses) {
                Integer stuId = stuCourse2.getStuId();
                //根据学生id查询课程名称
                Student student = studentDao.getById(stuId);
                map.put(student.getStuName(),stuCourse2.getScore());
                sum+=stuCourse2.getScore();
            }
            courseStudents.setMap(map);
            courseStudents.setAvg((Integer)sum/stuCourses.size());
            courseStudents.setCount(stuCourses.size());
            //转换为json
            String jsonString = JSON.toJSONString(courseStudents);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }

    }
}
